package com.softmaker.controller;

import com.softmaker.entity.Product;
import com.softmaker.entity.Video;
import com.softmaker.service.ProductService;
import com.softmaker.utils.ResultVOUtil;
import com.softmaker.vo.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.util.List;
import java.util.UUID;

@RestController
@RequestMapping("/product")
public class ProductController {
    @Autowired
    private ProductService service;
    @PostMapping("/addProduct")
    public Result<Product> addProduct(@RequestBody Product product) {
        if(service.findProduct(product.getName())!= null){
            return ResultVOUtil.error("商品已存在");
        }
        return ResultVOUtil.success(service.addProduct(product));
    }
    @GetMapping("/listProduct")
    public Result<List<Product>> listProduct() {
        return ResultVOUtil.success(service.listProduct());
    }
    @PostMapping("/addPhoto")
    public Result<String> addPhoto(@RequestParam("file") MultipartFile file , @RequestParam("name") String name) throws IOException {
        Product product = service.findProduct(name);
        String fileName = file.getOriginalFilename();
        // 设置临时文件路径
        // Windows路径
//        String tmpFilePath = "E:\\file\\";
        // Linux路径
        String tmpFilePath ="/home/server/video/";
        File temp = new File(tmpFilePath);
        if(!temp.exists()){
            temp.mkdirs();
        }
        // 获取文件后缀名
        Integer endIndexOf = fileName.lastIndexOf(".");
        // 上传文件后的后缀名
        String endFile = fileName.substring(endIndexOf, fileName.length());
        // 新名字
        String newName = UUID.randomUUID().toString() + endFile;
        String path = tmpFilePath + newName;
        product.setPhoto(path);
        // 转移到临时文件路径
        File localFile = new File(path);
        file.transferTo(localFile);
        // 添加视频到数据库
        service.updProduct(product);
        return ResultVOUtil.success("成功");
    }
    @GetMapping("/getProduct")
    public Result<Product> getProduct(@RequestParam("name") String name) {
        if(service.findProduct(name)!= null){
            return ResultVOUtil.success(service.findProduct(name));
        }
        else return ResultVOUtil.error("没有该商品");
    }
    @PostMapping("/updProduct")
    public Result<Product> updProduct(@RequestBody Product product) {
        return ResultVOUtil.success(service.updProduct(product));
    }
    @PostMapping("/getPhoto")
    public Result<String> getVideo(@RequestParam("name") String name, HttpServletResponse response) throws IOException {
        // 根据视频名称获取视频路径
        String path = service.findProduct(name).getPhoto();
        // 创建文件对象
        File files = new File(path);
        // 创建字节数组
        byte[] bytes = new byte[1024];
        try (OutputStream os = response.getOutputStream();
             FileInputStream fis = new FileInputStream(files)) {
            // 读取文件内容并写入输出流
            while ((fis.read(bytes)) != -1) {
                os.write(bytes);
                os.flush();
            }
        }
        // 返回成功结果
        return ResultVOUtil.success("成功");
    }
}
